package com.sds.sweng.swat.miplatform.service.pages;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Node;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

import com.sds.sweng.swat.miplatform.service.editors.MiplatformServiceEditor;
import com.sds.sweng.swat.miplatform.service.figures.components.SelectableComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.ValidationComponent;
import com.sds.sweng.swat.miplatform.service.layouts.MiplatformServicePageLayout;
import com.sds.sweng.swat.miplatform.service.validation.ValidationInfo;

public class MiplatformServicePage extends Composite {

	private MiplatformServiceEditor editor;

	private ServiceDesignArea serviceDesignArea;
	private ServiceControlArea serviceControlArea;

	private Document sourceXml;

	private static final int CONTROL_AREA_WIDTH = 300;

	private Node dsService;

	private String currentServiceId;

	public MiplatformServicePage(final Composite parent, MiplatformServiceEditor editor) {
		super(parent, SWT.NONE);

		this.editor = editor;

		initPage();
	}

	private void initPage() {
		MiplatformServicePageLayout layout = new MiplatformServicePageLayout(CONTROL_AREA_WIDTH);
		setLayout(layout);

		serviceDesignArea = new ServiceDesignArea(this);
		serviceControlArea = new ServiceControlArea(this);
	}

	public void setSourceXml(Document sourceXml) {
		this.sourceXml = sourceXml;

		dsService = sourceXml.selectSingleNode("//Datasets/Dataset[@Id='ds_service']");

		if (dsService == null) {
			// FIXME : create ds_service
			throw new RuntimeException("ds_service dataset must be exist.");
		}

		initValidationString();

		serviceDesignArea.display(sourceXml);
		serviceControlArea.bind(dsService);
	}

	public void setPartiedComponents(Node serviceRecord, String serviceId) {
		currentServiceId = serviceId;

		String serviceValidationString = serviceRecord.selectSingleNode("validation").getText();

		String[] validationStrings = StringUtils.split(serviceValidationString, ValidationInfo.validationSeparator);

		serviceDesignArea.initValidationComponentStyle(serviceId);

		for (int i=0;i<validationStrings.length;i++) {
			String componentId = getComponentId(validationStrings[i]);

			if (componentId != null) {
				serviceDesignArea.getValidationComponent(componentId).getValidationInfo(serviceId).setInfoFromString(validationStrings[i]);
				serviceDesignArea.getValidationComponent(componentId).refresh(serviceId);
			}
		}
	}

	public List<ValidationComponent> getPartiedValidationComponents(String serviceId) {
		return serviceDesignArea.getPartiedValidatioComponents(serviceId);
	}

	private String getComponentId(String validationString) {
		String[] infos = StringUtils.split(validationString, ValidationInfo.infoSeparator);

		for (int i=0;i<infos.length;i++) {
			if (infos[i].startsWith("id:")) {
				return StringUtils.substringAfter(infos[i], "id:");
			}
		}

		return null;
	}

	public void setSelected(SelectableComponent component) {
		if (currentServiceId != null)
			serviceControlArea.setSelectedComponent(component);
	}

	private void initValidationString() {
		Node validationNode = dsService.selectSingleNode("Contents/colinfo[@id='validation']");

		if (validationNode == null) {
			// FIXME : add validation column
			throw new RuntimeException("ds_service must have validation column.");
		}
	}

	public void replaceSource() {
		editor.replaceSource();
	}

}
